package org.zpmis.api.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zpmis.common.base.AdminLoginUser;
import org.zpmis.common.base.BaseWebService;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.domain.customer.enums.CustomerPlanEnum;
import org.zpmis.domain.customer.enums.ReturnVisitEnum;
import org.zpmis.domain.customer.model.CustomerPlan;
import org.zpmis.domain.customer.model.ReturnVisit;
import org.zpmis.domain.customer.qo.AddCustomerPlanQo;
import org.zpmis.domain.customer.qo.EditCustomerPlanQo;
import org.zpmis.domain.customer.qo.CustomerPlanListQo;
import org.zpmis.domain.customer.service.CustomerPlanService;
import org.zpmis.domain.customer.service.ReturnVisitService;
import org.zpmis.domain.customer.vo.CustomerPlanListVo;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with CodeGenerator
 * Description:
 * @author  Yang
 * Date: 2021-6-2
 * Time: 14:48:50
 */
@Service
@Slf4j
public class CustomerPlanWebService extends BaseWebService {
     @Reference
     private CustomerPlanService customerPlanService;

     @Reference
     private ReturnVisitService returnVisitService;

     /**
      * 新增意向客户回访计划
      * @param addCustomerPlanQo
      * @throws BusinessException
      * @throws Exception
      */
     @Transactional
     public void addCustomerPlan(AddCustomerPlanQo addCustomerPlanQo) throws BusinessException,Exception{

          //获取当前登录用户
          AdminLoginUser adminLoginUser = super.getAdminLoginUser();
          if(adminLoginUser == null){
               throw new BusinessException(ResultCode.USER_NOT_LOGIN);
          }
          Long visitId = addCustomerPlanQo.getVisitId();
          ReturnVisit returnVisit = returnVisitService.findById(visitId);
          if(returnVisit == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          //回访添加计划的时候 把 回访记录改成已完结状态
          returnVisitService.update(ReturnVisit.builder().id(visitId).status(ReturnVisitEnum.ENABLE_STATUS.code()).build());
          //新增计划信息
          customerPlanService.save(
               CustomerPlan.builder()
                       .intentionId(addCustomerPlanQo.getIntentionId())
                       .visitObject(CustomerPlanEnum.VISIT_OBJECT_INTENTION.code())
                       .chargePersonId(addCustomerPlanQo.getChargePersonId())
                       .chargePerson(addCustomerPlanQo.getChargePerson())
                       .visitId(addCustomerPlanQo.getVisitId())
                       .companyName(addCustomerPlanQo.getCompanyName())
                       .contactPerson(addCustomerPlanQo.getContactPerson())
                       .contactMobile(addCustomerPlanQo.getContactMobile())
                       .planDesc(addCustomerPlanQo.getPlanDesc())
                       .visitType(addCustomerPlanQo.getVisitType())
                       .visitTime(addCustomerPlanQo.getVisitTime())
                       .isDeleted(CustomerPlanEnum.NOT_DELETE.code())
                       .status(CustomerPlanEnum.DISABLE_STATUS.code())
                       .isRemind(addCustomerPlanQo.getIsRemind() == null ? CustomerPlanEnum.REMIND_YES.code() : addCustomerPlanQo.getIsRemind())
                       .remake(addCustomerPlanQo.getRemake())
                       .createId(adminLoginUser.getId())
                       .createPerson(adminLoginUser.getUserName())
                       .createTime(Instant.now().toEpochMilli())
                       .companyCode(returnVisit.getCompanyCode())
                       .build()
          );

     }


     /**
      * 回访计划列表查询
      * @param planListQo
      * @return
      */
     public PageInfo planList(CustomerPlanListQo planListQo) throws Exception{
          String chargePerson = planListQo.getChargePerson();
          String companyName = planListQo.getCompanyName();
          String contactPerson = planListQo.getContactPerson();
          String contactMobile = planListQo.getContactMobile();
          Long intentionId = planListQo.getIntentionId();
          Integer status = planListQo.getStatus();
          Long startTime = planListQo.getStartTime();
          Long endTime = planListQo.getEndTime();
          Integer visitType = planListQo.getVisitType();
          Integer visitObject = planListQo.getVisitObject();

          Condition condition = new Condition(CustomerPlan.class);
          Example.Criteria criteria = condition.createCriteria();
          criteria.andEqualTo("isDeleted",CustomerPlanEnum.NOT_DELETE.code());
          if(StringUtil.isNotEmpty(chargePerson)){
               criteria.andLike("chargePerson","%"+chargePerson+"%");
          }
          if(StringUtil.isNotEmpty(companyName)){
               criteria.andLike("companyName","%"+companyName+"%");
          }
          if(StringUtil.isNotEmpty(contactPerson)){
               criteria.andLike("contactPerson","%"+contactPerson+"%");
          }
          if(StringUtil.isNotEmpty(contactMobile)){
               criteria.andLike("contactMobile","%"+contactMobile+"%");
          }
          if(intentionId != null){
               criteria.andEqualTo("intentionId",intentionId);
          }
          if(visitObject != null){
               criteria.andEqualTo("visitObject",visitObject);
          }
          if(status != null){
               criteria.andEqualTo("status",status);
          }
          if(visitType != null){
               criteria.andEqualTo("visitType",visitType);
          }
          if(startTime != null && endTime != null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          if(startTime != null && endTime == null){
               criteria.andGreaterThanOrEqualTo("createTime",startTime);
               criteria.andLessThanOrEqualTo("createTime",Instant.now().toEpochMilli());
          }
          if(startTime == null && endTime != null){
               criteria.andLessThanOrEqualTo("createTime",endTime);
          }
          condition.orderBy("createTime").desc();
          PageHelper.startPage(planListQo.getPageNum(),planListQo.getPageSize());
          List<CustomerPlan> customerPlanList = customerPlanService.findByCondition(condition);
          PageInfo pageInfo = new PageInfo<>(customerPlanList);
          //设置自定义返回对象
          List<CustomerPlanListVo> resultList = customerPlanList.stream().map(CustomerPlanListVo::new).collect(Collectors.toList());
          pageInfo.setList(resultList);
          return pageInfo;
     }

     /**
      * 修改回访计划
      * @param editCustomerPlanQo
      */
     public void editCustomerPlan(EditCustomerPlanQo editCustomerPlanQo){
          Long id = editCustomerPlanQo.getId();
          CustomerPlan customerPlan = customerPlanService.findById(id);
          if(customerPlan == null){
               throw new BusinessException(ResultCode.DATA_NOT_FOUND);
          }
          customerPlan.setChargePersonId(editCustomerPlanQo.getChargePersonId());
          customerPlan.setChargePerson(editCustomerPlanQo.getChargePerson());
          customerPlan.setCompanyName(editCustomerPlanQo.getCompanyName());
          customerPlan.setContactPerson(editCustomerPlanQo.getContactPerson());
          customerPlan.setContactMobile(editCustomerPlanQo.getContactMobile());
          customerPlan.setPlanDesc(editCustomerPlanQo.getPlanDesc());
          customerPlan.setVisitType(editCustomerPlanQo.getVisitType());
          customerPlan.setVisitTime(editCustomerPlanQo.getVisitTime());
          customerPlan.setStatus(editCustomerPlanQo.getStatus());
          customerPlan.setIsRemind(editCustomerPlanQo.getIsRemind());
          customerPlan.setRemake(editCustomerPlanQo.getRemake());
          customerPlanService.update(customerPlan);
     }
}
